package com.gcxy.tour.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gcxy.tour.common.CommException;
import com.gcxy.tour.service.UserService;

@WebServlet(urlPatterns={"/user/changepwd.do"})
public class ChangePwdServlet  extends HttpServlet{


	private static final long serialVersionUID = -5796764944510461818L;

	private Logger logger=LoggerFactory.getLogger(getClass());
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		logger.debug("执行dopost方法");
		//1.收集参数
				try {
					String usercode=req.getParameter("usercode");
					String oldpassword=req.getParameter("password");
					String newpassword=req.getParameter("newpassword");
					String confirm=req.getParameter("confirm");
					UserService userService =new UserService();
				//2.类型转换
					
				//3.做参数校验
					if(usercode!=null&&usercode.length()>=4&&
					   oldpassword!=null&&oldpassword.length()>=6&&
					   newpassword!=null&& newpassword.length()>=6&&
					   confirm!=null&&confirm.length()>=6&&
					   confirm.equals(newpassword)) {
					}else {
						throw new CommException("修改的密码错误");
					}
				//4.调用业务方法
					userService.changePwd(usercode,oldpassword,newpassword);
					logger.debug("密码修改成功");
					resp.sendRedirect(req.getContextPath()+"/index.jsp");
				} catch (CommException e) {
					logger.error(e.getMessage());
					req.setAttribute("error", e.getMessage());
					req.getRequestDispatcher("/changepwd.jsp").forward(req, resp);
				}
		
		
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
		doGet(req, resp);
	}
	
}
